﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class StockFactory
{
    public static StockFactory Instance
    {
        get
        {
            if (m_Instance == null)
            {
                m_Instance = new StockFactory();
                m_Instance.Init();
            }
            return m_Instance;
        }
    }
    public static StockFactory m_Instance = null;

    public List<StockObjBase> m_Stockbjs = new List<StockObjBase>();
    public Dictionary<string, Stock> m_Stocks = new Dictionary<string, Stock>();
    public void Register<T>() where T : new()
    {
        m_Stockbjs.Add(new StockObj<T>());
    }
    void Init()
    {
        Register<LuanShu>();
        Register<RenDiYu>();
        Register<TenXun>();
        Register<SuoNi>();
        Register<WeiRuan>();
        Register<LaiYang>();
        Register<DaMi>();
        Register<NiFeng>();

        foreach (var obj in m_Stockbjs)
        {
            m_Stocks.Add(obj.m_Name, obj.CreateStock());
        }
    }

    public void Miao()
    {

    }
    public Stock CreateCharacter(string name)
    {
        for (int i = 0; i < m_Stockbjs.Count; ++i)
        {
            if (m_Stockbjs[i].m_Name == name)
            {
                return m_Stockbjs[i].CreateStock();
            }
        }
        return null;
    }

    public Stock RandomCreateOne()
    {
        return m_Stockbjs[UnityEngine.Random.Range(0, m_Stockbjs.Count)].CreateStock();
    }
}

public class StockObjBase
{

    public Stock m_Character;
    public string m_Name;
    virtual public Stock CreateStock()
    {
        return null;
    }
}
public class StockObj<T> : StockObjBase where T : new()
{
    public StockObj()
    {
        m_Character = new T() as Stock;
        m_Name = m_Character.m_ID.ToString();
    }
    public override Stock CreateStock()
    {
        Stock stock = new T() as Stock;
        stock.Init();
        return stock;
    }
}